module("luci.controller.admin.status", package.seeall)

function index()
    entry({"admin", "status"}, template("admin_status/status"), _("Status"), 1)
    entry({"admin", "status", "getStatInfo"}, call("getStatInfo_json"), nil, nil).leaf = true
end

function getStatInfo()

    local SysStatuArr={ }
    local wifimode={ }
    local wifissid={ }
    local encryptionmode={ }
    local wirelesschannel={ }
    local file =io.open("/tmp/wificapstat", "r")
    local capstat = "uncap"
    local encty=""
    local mode=""
    local ssid=""
    local channel=""
    local udiskInfo = luci.util.exec("df -h | grep 'sda1' | awk -F ' ' '{print $2,$5}'")
    local allsize=""
    local usedsize=""
    local X = uci.cursor()

    if(0 ~= string.len(udiskInfo)) then
       allsize = string.sub(udiskInfo, 1, string.find(udiskInfo, ' '))
       usedsize = string.sub(udiskInfo, string.find(udiskInfo, ' '), string.len(udiskInfo))
    end

    if file then
        capstat = file:read("*a")
        if(string.find(capstat,'1',1))
	    then
	        capstat="cap"

	    else
	        capstat="uncap"
	    end
        file:close()
    end

    X:foreach("wireless", "wifi-iface",
        function(s)
            if s.mode then
                mode=mode.."/"..s.mode
            end

            if s.ssid then
                ssid=ssid.."/"..s.ssid
            end

            if s.encryption then
                encty=encty.."/"..s.encryption
            end
        end)

    for val=0,2,1 do

        wirelesschannel[#wirelesschannel+1]=X:get("wireless", "radio"..val.."", "channel")
        if(0 ~= string.len(wirelesschannel[#wirelesschannel]))
        then
            channel=channel.."/"..wirelesschannel[#wirelesschannel]
        end
    end

    SysStatuArr[#SysStatuArr + 1] = {
        sysstate = "running",
       -- attackstate = "running",
        capturingstate = capstat,
        wifimode = mode,
        wifissid = ssid,
        encryptionmode = encty,
        wirelesschannel = channel,
        storeallsize = allsize,
        storeusedsize = usedsize,
        proeManagement = "connected"
    }
    return SysStatuArr
end

function getStatInfo_json()
    luci.http.prepare_content("application/json")
    luci.http.write('[')
    luci.http.write_json(getStatInfo())
    luci.http.write(']')
end
